package SocketDemo.TCP;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * @author:周鹏
 * @date: 2020/11/9 10:27
 * @description:
 */
public class NetClient {
    public static void main(String[] args) {
        OutputStream os=null;
        Socket socket=null;
        FileInputStream fs = null;
        {
            try {
                //服务器地址
               // inetAddress = InetAddress.getByName("127.0.0.1");
                //服务器端口
                //int port=9090;
                //建立与服务器链接
                socket = new Socket(InetAddress.getByName("127.0.0.1"),9090);
                //发送输出io流 output
                os = socket.getOutputStream();
                //文件输入客户端
                 fs=new FileInputStream(new File("aaa.txt"));
                int len=0;
                byte[] buffer=new byte[1024];
                while ((len=fs.read(buffer))!=-1){
                          os.write(buffer,0,len);
                }
               // 通知服务器输出完毕
                socket.shutdownOutput();
                //接受服务端消息
                ByteArrayOutputStream bos=new ByteArrayOutputStream();
                InputStream in=socket.getInputStream();
                int len2=0;
                byte[] buffer2=new byte[1024];
                while((len2=in.read(buffer2))!=-1){
                    bos.write(buffer2,0,len2);

                }
                System.out.println(bos.toString());

//                os.write("sssq安安心心".getBytes());
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                if (fs!=null){
                    try {
                        fs.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }
                if ((os!=null)){
                    try {
                        os.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    if(socket!=null){
                        try {
                            socket.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

                    }
                }
            }
        }
    }

